import { Form, Input, TreeSelect, DatePicker, Select, Col } from 'antd';
import moment from 'moment';
const SearchBuilder = (data: BasicListApi.Field[] | undefined) => {
  return (data || []).map((item) => {
    const basicAttr = {
      label: item.title,
      name: item.key,
      key: item.key,
    };
    switch (item.type) {
      case 'text':
        return (
          <Col sm={6} key={item.key}>
            <Form.Item {...basicAttr}>
              <Input disabled={item.disabled} />
            </Form.Item>
          </Col>
        );
      case 'tree':
        return (
          <Col sm={6} key={item.key}>
            <Form.Item {...basicAttr}>
              <TreeSelect treeData={item.data} disabled={item.disabled} treeCheckable />
            </Form.Item>
          </Col>
        );
      case 'datetime':
        if (item.key !== 'update_time') {
          return (
            <Col sm={12} key={item.key}>
              <Form.Item {...basicAttr}>
                <DatePicker.RangePicker
                  disabled={item.disabled}
                  showTime
                  style={{ width: '100%' }}
                  ranges={{
                    Today: [moment().startOf('day'), moment().endOf('day')],
                    'Last 7 Days': [moment().subtract(7, 'd'), moment()],
                    'Last 30 Days': [moment().subtract(30, 'days'), moment()],
                    'Last Month': [
                      moment().subtract(1, 'months').startOf('month'),
                      moment().subtract(1, 'months').endOf('month'),
                    ],
                  }}
                />
              </Form.Item>
            </Col>
          );
        }
        return null;
      case 'select':
      case 'switch':
        return (
          <Col sm={6} key={item.key}>
            <Form.Item {...basicAttr} valuePropName="checked">
              <Select>
                {(item.data || []).map((Option: any) => {
                  return (
                    <Select.Option value={Option.value} key={Option.value}>
                      {Option.title}
                    </Select.Option>
                  );
                })}
              </Select>
            </Form.Item>
          </Col>
        );
      default:
        return null;
    }
  });
};
export default SearchBuilder;
